""" System imports """
import os, sys
import logging

""" Other modules """
import pygame

""" Game """
import resourcemanager
from constants import *
from eventrouter import eventpanel
from items import *
import npc

""" Set up logging """
_log = logging.getLogger("epicquest.npc.shopkeeper")


class Shopkeeper(npc.NPC):
    
    def __init__(self, *args, **kwargs):    
        npc.NPC.__init__(self, *args, **kwargs)
        self.coords = kwargs.get('coords', (0, 0))
        self.name = kwargs.get('name', "Skip")
        self.inventory.addItem(Money(count=100))
        
        """ Events """
        eventpanel.subscribe(self, eventpanel.PLAYER_MOVE, self.onPlayerMove)
        
    def onPlayerMove(self, *args, **kwargs):
        player = kwargs.get('player', None)
        if player.isWithin(self, 1):
            self.giveMoney(player)
            
    def update(self, view_rect):
        if self.rect.colliderect(view_rect):
            self.dirty = 1
    
    def giveMoney(self, player):
        money = Money(count=10)
        if self.inventory.hasItem(money, True):
            self.inventory.giveItem(money, player.inventory)
            _log.debug("I now have " + str(self.inventory.countItem(Money())) + " moneys left.")
        else:
            _log.debug("I'm outta money! Bugger off!")
            
        
    
